import React from 'react'
import { BrowserRouter as Router, Redirect, Route, NavLink, Switch } from 'react-router-dom';
import './my.css'
import { ActivityIndicator } from 'antd-mobile'
class Mymysuibi extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
			userid: localStorage.getItem("userid"),
			res: [],
			isShow: true
			//显示this.state.res.map is not a function就是因为
			//应该是[]不是{}
  	    };
    }
	//查所有随笔
	//window.location.reload(true)与componentDidMount结合，实现刷新网页的功能
	componentDidMount() {
		//
		//console.log('topicschaall');
		fetch('http://62.234.30.197:8088/topics/idcha', {
			method: 'post',
			body: JSON.stringify({'userid':`${this.state.userid}`}),
			headers: {
			    "Access-Control-Allow-Origin":"*",
			    'content-type': 'application/json'
			},
			mode: 'cors'
		})
		.then(res=>res.json())
		.then(res=> {
			//console.log(res)
			this.setState({
				res: res,
				isShow: false
			});
		})
	}
	shan = (a) => {
		//console.log(a);
	}
    render () {
		const han = (id) => {
			fetch('http://62.234.30.197:8088/topics/topicsidshan', {
				method: 'post',
				body: JSON.stringify({'topicsid':id}),
				headers: {
				    "Access-Control-Allow-Origin":"*",
				    'content-type': 'application/json'
				},
				mode: 'cors'
			})
		}
		return (
        <div>
			 
			<div className='Mymsgsetnamesig-top'>
				<span className='my-return' onClick={() => this.props.history.push('/Mymsg')}>&lt;</span>
				<span className='my-setnamesig-top'>我的随笔</span>
			</div>
        	<div><ul>{
        		this.state.res.map((item, idx) => {
					////console.log(item.topicsid);
					var userid = `${item.userid}`;
					////console.log(`${item.pic}`);
					////console.log(userid);
        				return <NavLink key={idx} to={{pathname:'/notemsg/:' + item.topicsid, state: {item:item}}}>,
						<br />随笔 {idx + 1} :
						<li className='book-show' key={idx}>
        					<div className='' className='my-namesig-box'>
								<img src={item.pic} className='my-suibi-pic'/>
        						<br /><div className='item-txt'>{item.txt}</div>
        						<span>{item.writedate}</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
								<span>喜欢{item.likecount}</span>
								
								<button className='btn-shan' onClick={(e) => {
									e.stopPropagation();
									e.nativeEvent.stopImmediatePropagation();
									e.preventDefault();
									
									let arr = this.state.res;
									for(let i = 0; i < arr.length; i ++){
										if(arr[i].topicsid == item.topicsid){
											arr.splice(i, 1);
											break;
										}
									}
									this.setState({
										res: arr
									})
									han(item.topicsid);
									
									
									// this.props.history.push('/Mymysuibi');
									// this.props.history.goBack()
									//window.location.reload(true)
								}}>删除</button>
        					</div>
							<br /><br />
        				</li>
						</NavLink>
        		})
        	}</ul></div>
			<div style={{height:'300px'}}></div>
        	<div id="container"></div>
        </div>
    );
  }
}
export default Mymysuibi
//<button onClick={this.topicschaall}>查这个userid的所有随笔</button><br /><br />